package com.healthysys.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.healthysys.backend.exception.InsertException;
import com.healthysys.backend.mapper.RoleMapper;
import com.healthysys.backend.mapper.UserPoMapper;
import com.healthysys.backend.mapper.UserRoleAuthorityVOMapper;
import com.healthysys.backend.mapper.UserRoleMapper;
import com.healthysys.backend.service.UserPoService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pojo.dto.UserPODTO;
import pojo.dto.UserRoleAuthorityVOPageDTO;
import pojo.entity.UserPO;
import pojo.entity.UserRole;
import pojo.result.PageResult;
import pojo.vo.UserRoleAuthorityVO;

import java.util.List;

@Service
@Slf4j
@Transactional
public class UserPoImpl extends ServiceImpl<UserPoMapper, UserPO> implements UserPoService {

    @Resource
    private UserPoMapper userPoMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private UserRoleAuthorityVOMapper userRoleAuthorityVOMapper;


    @Override
    public void addUserPO(UserPODTO userPODTO) {
        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(userPODTO, userPO);
        int insertUserFlag = userPoMapper.insert(userPO);
        if (insertUserFlag != 1) { // 插入成功应返回影响行数为1，否则失败
            log.error("添加用户失败");
            throw new InsertException("添加用户失败");
        }

        Integer roleId = roleMapper.selectIdByRoleName(userPODTO.getRole());
        if (roleId == null) {
            log.error("角色不存在: {}", userPODTO.getRole());
            throw new InsertException("指定的角色不存在");
        }

        UserRole userRole = new UserRole(null, userPO.getId(), roleId); // 使用userPO.getId()获取刚插入用户的主键
        int insertUserRoleFlag = userRoleMapper.insert(userRole);
        if (insertUserRoleFlag != 1) {
            log.error("添加用户角色失败");
            throw new InsertException("添加用户角色失败");
        }
    }

    @Override
    public PageResult<List<UserRoleAuthorityVO>> getUserRoleAuthorityVO(UserRoleAuthorityVOPageDTO userRoleAuthorityVOPageDTO) {
        log.info("正在查询用户");

        String username = userRoleAuthorityVOPageDTO.getUsername();
        String role = userRoleAuthorityVOPageDTO.getRole();
        String authority = userRoleAuthorityVOPageDTO.getAuthority();

        // 使用 QueryWrapper 构建查询条件
        QueryWrapper<UserRoleAuthorityVO> queryWrapper = new QueryWrapper<>();

        // 添加模糊查询条件
        if (username != null && !username.isEmpty()) {
            queryWrapper.like("username", username);
        }
        if (role != null && !role.isEmpty()) {
            queryWrapper.like("role_name", role); // 假设字段名为 role_name
        }
        if (authority != null && !authority.isEmpty()) {
            queryWrapper.like("authority", authority);
        }

        // 分页查询
        PageHelper.startPage(userRoleAuthorityVOPageDTO.getPageNum(), userRoleAuthorityVOPageDTO.getPageSize());
        List<UserRoleAuthorityVO> userRoleAuthorityVOList = userRoleAuthorityVOMapper.selectList(queryWrapper);

        PageInfo<UserRoleAuthorityVO> pageInfo = new PageInfo<>(userRoleAuthorityVOList);

        return PageResult.success(pageInfo.getTotal(), pageInfo.getList());
    }


}
